package com.weitu.threeSimultaneities.projectInfo.controller;

import com.weitu.accessory.bean.AccessoryPackageInfo;
import com.weitu.accessory.service.AccessoryUtilService;
import com.weitu.accessory.util.AccessoryUtil;
import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.paging.DataGridPagination;
import com.weitu.plugin.PageView;
import com.weitu.threeSimultaneities.projectInfo.entity.Project;
import com.weitu.threeSimultaneities.projectInfo.service.ProjectService;
import com.weitu.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.weitu.util.AjaxResult.fail;
import static com.weitu.util.AjaxResult.success;

/**
 * Created by WT on 2017/4/5.
 */
@Controller
@RequestMapping(value = "threeSimultaneities/projectInfo")
public class ProjectController extends BaseController {
    @Autowired
    private ProjectService projectService;
    @Autowired
    private AccessoryUtilService accessoryUtilService;

    /***
     * 跳转至列表页面
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list() {
        return BASE_JSP_PATH + "threeSimultaneities/projectInfo/list";
    }

    /***
     * 获取分页数据
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    public Object listAjax(HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        putParametersIntoMap(map);
        //查询数据总数
        int count = projectService.selectByParamCount(map);
        //获取分页对象
        DataGridPagination Pagination = DataGridPagination.getInstance(request, count);
        PageView pageView = Pagination.getPageView();//生成pageView分页对象

        map.put("pageView", pageView);
        List<Project> dataList = projectService.selectByParam(map);
        return Pagination.flushDataGridData(dataList);
    }

    /***
     * 新增项目页面
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add() {
        return BASE_JSP_PATH + "threeSimultaneities/projectInfo/add";
    }

    /***
     * 添加项目信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @Transactional(readOnly = false)
    @SystemLog(module = "项目基本信息管理", methods = "添加项目")
    public AjaxResult insertSelective() {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        getAddBasicFieldMap(paramMap);
        paramMap.put("delState", 0);
        int boo = projectService.addProject(paramMap);
        boolean accFlag = AccessoryUtil.saveAccerssoryInfo(paramMap.get("id").toString());
        if (boo == 1 && accFlag) {
            return success("添加成功");
        } else {
            return fail("添加失败");
        }
    }

    /***
     * 项目信息编辑页面
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String editAjax(Model model, String id,String type) throws Exception {
        List<AccessoryPackageInfo> accessoryList = accessoryUtilService.queryAccessoryByBusinessId(id);
        model.addAttribute("project", projectService.selectById(id));
        model.addAttribute("accessoryList", accessoryList);
        return BASE_JSP_PATH + "threeSimultaneities/projectInfo/"+type;
    }

    @SystemLog(module = "项目基本信息管理", methods = "编辑项目信息")
    @ResponseBody
    @Transactional(readOnly = false)
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public AjaxResult editAjax() throws Exception {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        getEditBasicFieldMap(paramMap);
        int boo = projectService.updateById(paramMap);
        boolean accFlag = AccessoryUtil.saveAccerssoryInfo(paramMap.get("id").toString());
        if (boo == 1 && accFlag) {
            return success("更新成功！");
        } else {
            return fail("更新失败！");
        }
    }


    /***
     * 删除项目
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("delete")
    @Transactional(readOnly = false)
    @SystemLog(module = "项目基本信息管理", methods = "删除项目信息")
    public AjaxResult deleteById(String id) {
        int boo = projectService.deleteById(id);
        if (boo > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }
    }

    /***
     * 查询项目
     * @return
     */
    @RequestMapping(value = "/projectQueryList")
    public String projectQueryList() {
        return BASE_JSP_PATH + "threeSimultaneities/projectInfo/queryProject";
    }

}
